package com.zzty.txj1.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.zzty.txj1.config.WeiChatConfig;
import com.zzty.txj1.entity.User;
import com.zzty.txj1.service.UserService;
import com.zzty.txj1.util.HttpUtil;
import netscape.javascript.JSObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Value("${url.user.getUserById}")
    String url;
    @Value("${url.user.getUserByOpenId}")
    String getUserByOpenId;
    @Value("${url.user.getUserByClazzId}")
    String getUserByClazzId;
    @Value("${url.user.getAllUsers}")
    String getAllUsers;

    @Value("${url.user.getUserByName}")
    String url3;

    @Override
    public List<User> getUserByClazzId(Integer clazzId) {
        List<User> users = new ArrayList<User>();
        try {
            String result = HttpUtil.httpGet(WeiChatConfig.urlToUser + getUserByClazzId + "?id=" + clazzId);
            users = JSONObject.parseArray(result, User.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return users;
    }

    public User getUserById(Integer id) {
        User user = null;
        try {
            String result = HttpUtil.httpGet(WeiChatConfig.urlToUser + url + id);
            user = JSONObject.parseObject(result, User.class);
        } catch (Exception ex) {

        }
        return user;
    }

    public User getUserById2(Integer id) {
        User user = null;
        try {
            String result = HttpUtil.httpGet(WeiChatConfig.urlToUser + url + id);
            user = JSONObject.parseObject(result, User.class);
        } catch (Exception ex) {

        }
        return user;
    }

    @Override
    public User getUserByOpenId(String openId) {
        User user = null;
        try {
            String result = HttpUtil.httpGet(WeiChatConfig.urlToUser + getUserByOpenId + openId);
            user = JSONObject.parseObject(result, User.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return user;
    }

    @Override
    public User getUserByName(String name) {
        User user = null;
        String result = null;
        try {
            result = HttpUtil.httpGet(WeiChatConfig.urlToUser + url3 + name);
            user = JSONObject.parseObject(result, User.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("user===" + user.toString());
        return user;
    }

    @Override
    public List<User> getAllUsers() {
        List<User> users = new ArrayList<User>();
        try {
            String result = HttpUtil.httpGet(WeiChatConfig.urlToUser + getAllUsers);
            users = JSONObject.parseArray(result, User.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return users;
    }

    @Override
    public Integer updateUser(User user) {
        return null;
    }
}
